package com.bwie.controller;

import com.bwie.config.MinoConfig;
import com.bwie.utils.R;
import io.minio.ObjectWriteResponse;
import io.minio.PutObjectArgs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * TODO
 *
 * @Description
 * @Author 36167
 * @Date 2025/3/11 16:29
 **/


@RestController
@RequestMapping("/min")
public class MinioController {

    @Autowired
    private MinoConfig minoConfig;

    @PostMapping("/upload")
    public R upload(MultipartFile file){
        try {
            ObjectWriteResponse bucket = minoConfig.minioClient()
                   .putObject(PutObjectArgs.builder().bucket("day3-11")
                    .object(file.getOriginalFilename())
                    .stream(file.getInputStream(), file.getSize(), -1).build());
         String Url=" http://127.0.0.1:9000/";
         String format=String.format("%s%s%s",Url,bucket.bucket()+"/",bucket.object());
          return R.OK(format);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ERROR(500,"上传失败");
    }

  @PostMapping("/upload1")
    public R upload1(MultipartFile file){

      try {
          ObjectWriteResponse response = minoConfig.minioClient().putObject(PutObjectArgs.builder()
                  .bucket("day3-11").object(file.getOriginalFilename())
                  .stream(file.getInputStream(), file.getSize(), -1).build());
          String url="http://127.0.0.1:9000/";
          String format=String.format("%s%s%Ws",url,response.bucket()+"/"+response.object());
          return R.OK(format);
      } catch (Exception e) {
          throw new RuntimeException(e);
      }
  }






}
